﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DataAccess.IRepositories;
using System.ComponentModel.Composition;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IFolderRepository))]
    public class FolderRepository : IFolderRepository
    {
        private Connection conn;

        public FolderRepository()
        {
            conn = new Connection();
        }

        public Folder GetFolderByAccountID(int AccountID)
        {
            Folder result = new Folder();
            using (MXHEntities context = conn.GetContext())
            {
                var folders = from n in context.Folders
                                where n.AccountID == AccountID
                                orderby n.CreateDate descending
                                select n;
                try
                {
                    if (folders != null)
                        result = folders.First();
                }
                catch (Exception e) { result = null; }
            }
            return result;
        }


        public Folder GetFolderByID(Guid FolderID)
        {
            Folder folder;
            using (MXHEntities context = conn.GetContext())
            {
                folder = context.Folders.Where(n => n.FolderID == FolderID).FirstOrDefault();
            }
            return folder;
        }


        public Guid SaveFolder(Folder folder)
        {
            Guid result;
            using (MXHEntities context = conn.GetContext())
            {
                context.Folders.AddObject(folder);
                context.SaveChanges();
                result = folder.FolderID;
            }
            return result;
        }


        public Folder GetNewestFolderID(int accountID)
        {
            Folder result = new Folder();
            using (MXHEntities context = conn.GetContext())
            {
                result = context.Folders.Where(n => n.AccountID == accountID).OrderByDescending(n => n.CreateDate).FirstOrDefault();
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountIds"></param>
        /// <returns></returns>
        public List<Guid> GetFolderIdByAccountId(List<int> accountIds)
        {
            List<Guid> result = new List<Guid>();            
            using (MXHEntities context = conn.GetContext())
            {
                if (accountIds.Count > 0)
                {
                    //accountIds.ForEach(n => result.Add(context.Folders.FirstOrDefault(i => i.AccountID == n) != null ? context.Folders.FirstOrDefault(i => i.AccountID == n).FolderID : Guid.Empty));
                    foreach (int accID in accountIds)
                    {
                        var item = from fo in context.Folders
                                   where fo.AccountID == accID
                                   select fo.FolderID;
                        if (item.Count() > 0)                      
                            result.AddRange(item.ToList());
                    }
                }
            }            
            return result;
        }
    }
}
